home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 October / Macformat17.cdr / Shareware City / Developers / shutdown-fx-20-c / sfx control app ƒ / sfx code ƒ / sfx install.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-11  |  4.6 KB  |  161 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        sfx install.c
  4.  
  5. Purpose:    This module handles installing shutdown procs and installing
  6.             and removing fades from the system heap.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "Shutdown.h"
  26. #include "sfx install.h"
  27. #include "sfx gestalt.h"
  28. #include "file interface.h"
  29. #include "util.h"
  30.  
  31. enum ErrorTypes InstallTheShutdownProc(short index, Str255 theName, Boolean onShutdown,
  32.     Boolean onRestart, Boolean installFade, Boolean installProcs)
  33. /* assume: there are no shutdown procs currently installed */
  34. /* assume: sfx Gestalt function is properly installed */
  35. {
  36.     OSErr            theResError;
  37.     FSSpec            fs;
  38.     short            oldRefNum, newRefNum;
  39.     Boolean            alreadyOpen;
  40.     Handle            theProcHandle;
  41.     ProcPtr            theFade, theShutdownProc, theRestartProc;
  42.     unsigned long    theSize;
  43.     unsigned long    dummy;
  44.     enum ErrorTypes    resultCode;
  45.     
  46.     if (installFade)
  47.     {
  48.         MyMakeFSSpec(gModuleVRefNum, gModuleDirID, theName, &fs);
  49.         theResError=OpenTheResFile(&fs, &oldRefNum, &newRefNum, &alreadyOpen);
  50.         if (theResError!=noErr)
  51.             return kCantOpenModule;
  52.         
  53.         theProcHandle=Get1Resource('PROC', 0);
  54.         if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  55.             return kModuleDamaged;
  56.             
  57.         if (*theProcHandle==0L)
  58.             LoadResource(theProcHandle);
  59.         if (*theProcHandle==0L)
  60.             return kModuleDamaged;
  61.             
  62.         HLock(theProcHandle);
  63.         theFade=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  64.         Mymemcpy(theFade, *theProcHandle, theSize);
  65.         ReleaseResource(theProcHandle);
  66.         theProcHandle=0L;
  67.         CloseTheResFile(oldRefNum, newRefNum, alreadyOpen);
  68.     }
  69.     else theFade=0L;
  70.     
  71.     if (installProcs)
  72.     {
  73.         theProcHandle=Get1Resource('PROC', 10);    /* get shutdown proc from sfx application */
  74.         if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  75.             return kCantGetProc10Resource;
  76.     
  77.         if (*theProcHandle==0L)
  78.             LoadResource(theProcHandle);
  79.         if (*theProcHandle==0L)
  80.         {
  81.             DisposePtr(theFade);
  82.             return kCantGetProc10Resource;
  83.         }
  84.         
  85.         theShutdownProc=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  86.         theRestartProc=NewPtrSys(theSize);
  87.         Mymemcpy(theShutdownProc, *theProcHandle, theSize);
  88.         Mymemcpy(theRestartProc, *theProcHandle, theSize);
  89.         ReleaseResource(theProcHandle);
  90.         theProcHandle=0L;
  91.     }
  92.     else
  93.     {
  94.         resultCode=GetGestaltProcPtrs(&dummy, &theShutdownProc, &theRestartProc);
  95.         if (resultCode!=allsWell)
  96.             return resultCode;
  97.     }
  98.     
  99.     *(ProcPtr*)((long)theShutdownProc+6)=
  100.         *(ProcPtr*)((long)theRestartProc+6)=theFade;            /* store address of fade */
  101.     *(short*)((long)theShutdownProc+10)=onShutdown ? 1 : 0;        /* store on shutdown flag */
  102.     *(short*)((long)theRestartProc+10)=onRestart ? 1 : 0;        /* store on restart flag */
  103.     if (installProcs)
  104.     {
  105.         ShutDwnInstall(theShutdownProc, sdOnPowerOff);
  106.         ShutDwnInstall(theRestartProc, sdOnRestart);
  107.     }
  108.     gModuleIndex=index;
  109.     resultCode=SetGestaltProcPtrs(theFade, theShutdownProc, theRestartProc);
  110.     
  111.     return resultCode;
  112. }
  113.  
  114. enum ErrorTypes RemoveTheFade(void)
  115. {
  116.     ProcPtr            theFade, theShutdownProc, theRestartProc;
  117.     enum ErrorTypes    resultCode;
  118.     
  119.     resultCode=GetGestaltProcPtrs(&theFade, &theShutdownProc, &theRestartProc);
  120.     if (resultCode!=allsWell)
  121.         return resultCode;
  122.  
  123.     resultCode=SetGestaltProcPtrs(0L, theShutdownProc, theRestartProc);
  124.     if (resultCode!=allsWell)
  125.         return resultCode;
  126.     
  127.     if (theFade!=0L)
  128.         DisposePtr(theFade);
  129.     
  130.     if (theShutdownProc!=0L)
  131.         *(short*)((long)theShutdownProc+10)=0;
  132.     
  133.     if (theRestartProc!=0L)
  134.         *(short*)((long)theRestartProc+10)=0;
  135.     
  136.     return allsWell;
  137. }
  138.  
  139. OSErr OpenTheResFile(FSSpec* fs, short* oldRefNum, short* newRefNum, short* alreadyOpen)
  140. {
  141.     unsigned long    oldTopMapHndl;
  142.     OSErr            theResError;
  143.     
  144.     *oldRefNum=CurResFile();
  145.     oldTopMapHndl=(unsigned long)TopMapHndl;
  146.     *newRefNum=HOpenResFile(fs->vRefNum, fs->parID, fs->name, fsRdPerm);
  147.     theResError=ResError();
  148.     *alreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
  149.     return theResError;
  150. }
  151.  
  152. void CloseTheResFile(short oldRefNum, short newRefNum, Boolean alreadyOpen)
  153. {
  154.     if (!alreadyOpen)
  155.     {
  156.         CloseResFile(newRefNum);
  157.     }
  158.     UseResFile(oldRefNum);
  159. }
  160.  
  161.